//
//  MusicListViewController.m
//  老司机发车了
//
//  Created by SuperNing on 16/8/6.
//  Copyright © 2016年 斌. All rights reserved.
//

#import "MusicListViewController.h"

#import "SingerListModel.h"
#import "HotSingerCollectionViewCell.h"
#import "SingerMainListTableViewCell.h"

#import "DetailSongTableViewController.h"
#import "SingerListTableViewController.h"

@interface MusicListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *collectionArray;


@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong) NSArray *sectionArray;

@property (nonatomic, strong)NSArray *rowTitleArray;

@end

@implementation MusicListViewController

- (NSMutableArray *)collectionArray
{
    if (!_collectionArray) {
        _collectionArray = [[NSMutableArray alloc] init];
    }
    return _collectionArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.sectionArray = @[@"华语乐坛",@"欧美流行",@"韩语乐坛",@"日语乐坛",@"其他"];
    self.rowTitleArray = @[@[@"华语男歌手",@"华语女歌手",@"华语乐队组合"],@[@"欧美男歌手",@"欧美女歌手",@"欧美乐队组合"],@[@"韩国男歌手",@"韩国女歌手",@"韩国乐队组合"],@[@"日本男歌手",@"日本女歌手",@"日本乐队组合"],@[@"其他"]];
    
    [self analyseCollectionData];
    [self addCollectionView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SingerMainListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    blurView.frame = self.view.bounds;

    self.tableView.backgroundView = blurView;
    
}



- (void)analyseCollectionData{
    
    [NetWorkRequestManager requestWithType:POST urlString:HotSinger_Url parDic:@{} finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        for (NSDictionary *dict in dataDic[@"artist"]) {
            SingerListModel *model = [[SingerListModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.collectionArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
           
            self.tableView.tableHeaderView = self.collectionView;
            [self.collectionView reloadData];
        });
        
    } finishError:^(NSError *error) {
        
    }];
    
    
}


- (void)addCollectionView{
    
    _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 250) collectionViewLayout:_flowLayout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    _flowLayout.headerReferenceSize = CGSizeMake(0, 30);
    
    [_collectionView registerNib:[UINib nibWithNibName:@"HotSingerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerCell"];
    
}

#pragma mark-----collectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.collectionArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 30, 0, 30);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerCell" forIndexPath:indexPath];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"热门歌手";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.centerX = screenWidth / 2;
    [reusableView addSubview:titleLabel];
    return reusableView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(80, 100);
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HotSingerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    SingerListModel *model = self.collectionArray[indexPath.row];
    cell.picView.layer.cornerRadius = cell.picView.ownWidth / 2;
    cell.picView.layer.masksToBounds = YES;
    
    [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.avatar_big]];
    
    
    cell.nameLabel.text = model.name;
    
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SingerListModel *model = self.collectionArray[indexPath.row];
    DetailSongTableViewController *detailVC = [[DetailSongTableViewController alloc] init];
    
    detailVC.jsonNum = 2;
    detailVC.picUrl = model.avatar_big;
    detailVC.tinguid = model.ting_uid;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark-----tableviewDatasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 4) {
        return 1;
    } else {
        return 3;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return self.sectionArray[section];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SingerMainListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.rowTitleLabel.text = self.rowTitleArray[indexPath.section][indexPath.row];
    
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 55;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SingerListTableViewController *detailVC = [[SingerListTableViewController alloc] init];
    
    if (indexPath.section == 0) {
        detailVC.areanum = 6;
        detailVC.sexNum = indexPath.row + 1;
    }else if(indexPath.section == 1){
        detailVC.areanum = 3;
        detailVC.sexNum = indexPath.row + 1;
    }else if(indexPath.section == 2){
        detailVC.areanum = 7;
        detailVC.sexNum = indexPath.row + 1;
    }else if(indexPath.section == 3){
        detailVC.areanum = 60;
        detailVC.sexNum = indexPath.row + 1;
    }else{
        detailVC.areanum = 5;
        detailVC.sexNum = 4;
    }

    [self.navigationController pushViewController:detailVC animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
